package com.xj.opcua;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.xj.opcua.client.Client;
import com.xj.opcua.client.util.OpcUaClentUtils;

@Service
public class GlobalBoostrapService implements ApplicationRunner{
	
	@Override
	public void run(ApplicationArguments args) throws Exception {
		ExecutorService es = Executors.newFixedThreadPool(4);
		es.submit(() -> {
			String urlString = "opc.tcp://49.72.111.139:53534/OPCUA/SCADA";
			int namespaceIndex = 2;
			JSONObject result = OpcUaClentUtils.connectOpcUaClent(urlString);
			if(result.getInteger("code") == 200) {
				//订阅
				OpcUaClient client = (OpcUaClient) Client.map.get(urlString).get(0);
				NodeId nodeId = new NodeId(namespaceIndex, "Scada.Mes");
				List<NodeId> listNode = OpcUaClentUtils.browseNodeId("", client, nodeId, namespaceIndex);
				OpcUaClentUtils.createSubscription(urlString, client, listNode, 500.0);
			}
		});
        es.shutdown();
		
		//Map<String, List<Object[]>> map = new HashMap<String, List<Object[]>>();
		//List<Object[]> List = new ArrayList<>();
		//List.add(new Object[] {"mes1","mes1",""});
		//map.put("folder", List);
		//Server.opcNodesList.add(map);
		
		//List<Object[]> List1 = new ArrayList<>();
		//List1.add(new Object[] {"mes1.boolean","boolean","Boolean"});
		//List1.add(new Object[] {"mes1.int","int","Int32"});
		//List1.add(new Object[] {"mes1.oh","oh","String"});
		//List1.add(new Object[] {"mes1.float","float","Float"});
		//List1.add(new Object[] {"mes1.double","double","Double"});
		//List1.add(new Object[] {"mes1.short","short","Int16"});
		//map.put("nodes", List1);
		//Server.opcNodesList.add(map);
		
		//OpcUaServerManager opcUaServerManager = new OpcUaServerManager();
		//opcUaServerManager.bootstrap();
	}

}
